package com.touchstone.synchdata.common.util;

import cn.hutool.core.util.IdUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @Description : id生成工具
 * @Author : hunter
 * @Date: 2019-11-25 20:17
 * @Version: 1.0
 **/
@Component
public class IDUtils {

    /**
     * 数据中心
     */
    private static Long dataCenter;

    /**
     * 机器编号
     */
    private static Long work;

    @Value("${server.dataCenterId}")
    public void setDataCenterId(Long dataCenterId) {
        dataCenter = dataCenterId;
    }

    @Value("${server.workId}")
    public void setWorkId(Long workId) {
        work = workId;
    }

    public static Long getId(){
        /**
         * IdUtil.getSnowflake()已实现单例模式
         */
        return IdUtil.getSnowflake(IDUtils.dataCenter, IDUtils.work).nextId();
    }

}
